package com.tattermedia.allthataudio.views;

import hw.ats.news.R;

import java.util.HashMap;
import java.util.Map.Entry;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

/**
 * Audio player shared bitmap
 *
 */

public class SharedBitmap {
	
	private static SharedBitmap		mInstance;
	
	private Resources		mRes;
	
	private HashMap<Object, Bitmap> mBitmapMap = new HashMap<Object, Bitmap>();
	
	private SharedBitmap() {
		
	}
	
	public static SharedBitmap getInstance() {
		if (mInstance == null) {
			mInstance = new SharedBitmap();
		}
		
		return mInstance;
	}
	
	public void initialize(Resources res) {
		mRes = res;
		
		// load seek bar shared bitmap
		
		getBitmap(R.drawable.seekbar_background_left);
		getBitmap(R.drawable.seekbar_background_middle);
		getBitmap(R.drawable.seekbar_background_right);
		
		getBitmap(R.drawable.seekbar_buffered_left);
		getBitmap(R.drawable.seekbar_buffered_middle);
		getBitmap(R.drawable.seekbar_buffered_right);
		
		getBitmap(R.drawable.seekbar_played_left);
		getBitmap(R.drawable.seekbar_played_middle);
	}
	
	public void release() {
		for (Entry<Object, Bitmap> entry : mBitmapMap.entrySet()) {
			Bitmap bitmap = entry.getValue();
			bitmap.recycle();
			
		}
		
		mBitmapMap.clear();
	}
	
	public Bitmap getBitmap(int resId) {
		Bitmap bitmap = mBitmapMap.get(resId);
		
		if (bitmap == null) {
			bitmap = loadBitmap(resId);
			mBitmapMap.put(resId, bitmap);
		}
		
		return bitmap;
	}
	
	protected Bitmap loadBitmap(int resId) {
//		Options option = new Options();
//		option.inDither = false;
//		option.inPreferredConfig = Bitmap.Config.RGB_565;
//		option.inScaled = false;
		Bitmap bitmap = BitmapFactory.decodeResource(mRes, resId);
		return bitmap;
	}
}
